/**
 * Created by Administrator on 2018/1/17.
 */
define(function(require, exports, module) {
    var $=require('zepto');
        s=require('./stock');
        api=require('api');
        cn=require('conment');
        ck=require('cookie');
        module.exports = {
            in:{
                isSelect:'',
                // listHy:ck.getStorage.future_list,//JSON.parse(cn.getItem('contract')),
                getSelf:ck.getStorage.zx_astock,//JSON.parse(cn.getItem('getSelf')),
                code:"",
                name:"",
                type:false,
                free:cn.getQueryString('free'),
            },
            addSelfSelect:function(){
                const _this=this;
                _this.in.code=cn.getQueryString('code')||"000001";
                const code=(_this.in.code).toString()
                for(var i in _this.in.getSelf){
                    if(_this.in.getSelf[i]==code){
                        _this.in.type=true;
                        break;
                    }
                }
                if(_this.in.type){
                    $('#addSelect').text('删除');
                }
                $('#addSelect').on('click',function(){
                    _this.in.name=$("#astock-name").text();
                    var arr=code
                    var list_futrue=JSON.parse(cn.getItem('zx_astock'));

                    if(!_this.in.type){
                        if(list_futrue==null){
                            list_futrue=code.split(',')
                        }else{
                            list_futrue.unshift(arr)
                        }
                        cn.setItem('zx_astock',
                            JSON.stringify(list_futrue)
                        );
                        _this.in.type=true;
                        cn.alert('添加成功')
                        $('#addSelect').text('删除');
                    }else{
                        for(var i in list_futrue){
                            if(list_futrue[i]==code){
                                list_futrue.splice(i,1)
                                break;
                            }
                        }
                        cn.setItem('zx_astock',
                            JSON.stringify(list_futrue)
                        );
                        _this.in.type=false;
                        cn.alert('删除成功')
                        $('#addSelect').text('添加');
                    }
                })

                this._temslist()
            },
            _temslist:function () {
                var price=parseFloat($('#astock-price').text());
                const _this=this;
                if(!price){
                    setTimeout(function () {
                        _this._temslist()
                    },10)
                }else{
                    var stockNum=price*100;
                    var freeId=null,freeNum=0;
                    cn.Ajax(api.items,{
                        type:_this.in.free||'11',
                    },function(d){
                        if(d.flag==1){
                            var arr=new Array(),preBuyCount=0,percentage=0;
                            const list=d.list;
                            if(_this.in.free==10){
                                freeId=list[0].id;
                                var freeAmount=parseFloat(list[0].amount)
                                preBuyCount= Math.floor(( freeAmount/ price) / 100)*100;
                                $('#preBuyCount').text(preBuyCount);
                                $('#hzf').text(list[0].rate);
                                $('#hzyj').text(list[0].bond);
                                $('#more').text(list[0].typeStr)
                                $('#percentage').text((preBuyCount * price / freeAmount*100).toFixed(2));
                                $('.moneyFree').text(freeAmount);
                                freeNum=preBuyCount;
                                return
                            }
                            // console.log(list)
                            $.each(list,function(i,l,array){
                                preBuyCount= Math.floor((l.amount / price) / 100)*100;
                                percentage = (preBuyCount * price / l.amount*100).toFixed(2);//使用率
                                arr.push('<span class="'+(preBuyCount<100?'disable ':'')+(l.type==10?'free':'')+'" data-id="'+l.id+'" data-amount="'+l.amount+'" data-disable="'+(preBuyCount<100?'true':'false')+'">'+l.amountStr+'</span>');
                            });
                            $('#temsMoney').append(arr.join(''))
                            $('#temsMoney span').on('click',function () {
                                var amount=parseFloat($(this).data('amount'));
                                var buyCount=Math.floor((amount / price) / 100)*100;
                                if($(this).data('disable'))return false
                                $(this).addClass('optional').siblings('span').removeClass('optional');
                                $('#preBuyCount').text(buyCount)
                                $('#percentage').text((buyCount * price / amount*100).toFixed(2))
                            })
                        }else{
                            cn.alert(d.message)
                        }
                    },'post');

                    $("#more").on('click',function () {
                        var id=null;
                        if(_this.in.free==10){
                            id=freeId;
                            if(freeNum<100)return cn.alert('超出免费策略金额，请更换股票')
                            if(!id)return;
                        }else {
                            id=$('.optional').data('id');
                            if(!id)return cn.alert('请选择金额');
                        }
                        cn.isLoding=false;
                        cn.loadHtml({
                            txt:'寻找搭档',
                            time:1,
                            is:true,
                        });
                        cn.Ajax(api.buy,{
                            itemId:id,
                            stock:_this.in.code
                        },function(d){
                            cn.loadHtml({
                                is:false,
                            });
                            if(d.flag==1){
                                cn.setItem('buyDetali',
                                    JSON.stringify(d.data)
                                );
                                window.location.href="./buyDeal.html?code="+_this.in.code+"&id="+d.data.id
                            }else if(d.flag==-100){
                                cn.modelDialog({
                                    msg:'请先登录',
                                    btnValue:'取消',
                                    btn:[{
                                        txt:'确定',
                                        callBack:function(){
                                            window.location.href='/view/login.html'
                                        }
                                    }]
                                });
                            }else{
                                cn.alert(d.message)
                            }
                        },'post');
                        // cn.setItem('stock_url',cn.getHtmlDocName())
                        // window.location.href="./buyDeal.html?code="+stockcode+"&type=1"
                    });
                }
            },
            init:function () {
                var tm = Math.pow(10, 3);
                var stockcode =cn.getQueryString('code')||'000001';
                var timestamp = Date.parse(new Date()) / 1000;
                var type = "sz";
                var data={};

                function getNowFormatDate() {
                    var date = new Date();
                    var seperator1 = "-";
                    var year = date.getFullYear();
                    var month = date.getMonth() + 1;
                    var strDate = date.getDate();
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    if (strDate >= 0 && strDate <= 9) {
                        strDate = "0" + strDate;
                    }
                    var currentdate = year + seperator1 + month + seperator1 + strDate;
                    return currentdate;
                }
                data.stockID=stockcode;
                data.stockType=type;
                data.time=parseInt(timestamp);
                data.hq=eval('"hq_str_'+type+stockcode+'"');
                data.day=""+getNowFormatDate();
                data.isHqReload=eval("true");


                //菜单
                data.menu=[];

                data.menu.push({name:"minute",	title:"分时"});
                data.menu.push({name:"dayK",	title:"日K"});

                //默认加载
                data.default="minute";
                stock.load(data);



                $('#back').on('click',function () {
                    var back_url=cn.getItem('market_url');
                    localStorage.removeItem('market_url');
                    if(back_url==null||back_url==''){
                        window.location.href='./hot_nav.html'
                    }else{
                        window.location.href=back_url
                    }
                })
            },
        }

});